package com.coldchain.admin.resoure.api;

import com.coldchain.admin.resoure.entity.Resource;
import com.coldchain.admin.resoure.service.IResourceService;
import com.coldchain.authclient.annotation.CheckClientToken;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.SystemModelEnum;
import com.fangliai.core.exception.BaseException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-08-28
 */
@RestController
@RequestMapping("/resource")
@ApiModel
@CheckUserToken
@CheckClientToken
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @ApiOperation(value = "获取资源信息")
    @GetMapping("/list")
    public BaseResponse  listByCurrentSysModel()throws BaseException {
        return new ObjectRestResponse<>()
                .data(resourceService.listAllBySysModel(BaseContextHandler.getSysModl()));
    }

    @GetMapping("/listBySys")
    @ApiImplicitParam(value = "系统模块",name = "sysmodel",dataType = "string",required = true,paramType = "query")
    public BaseResponse listBySysModel(String sysmodel)throws BaseException {
        if(!SystemModelEnum.valueOf(BaseContextHandler.getSysModl()).equals(SystemModelEnum.ADMIN)){
             return  new ObjectRestResponse<>().error("访问受限");
        }
        return new ObjectRestResponse<>()
                .data(resourceService.listAllBySysModel(sysmodel));
    }

    @GetMapping("/del/{id}")
    @ApiOperation("删除资源")
    @ApiImplicitParam(value = "id",name="id",dataType = "integer",required = true,paramType = "path")
    public BaseResponse del(@PathVariable("id") Integer id){
        try {
            if(!SystemModelEnum.valueOf(BaseContextHandler.getSysModl()).equals(SystemModelEnum.ADMIN)){
                return  new ObjectRestResponse<>().error("访问受限");
            }
            resourceService.removeById(id);
            return new ObjectRestResponse<>().succeed();
        }catch (Exception e){
            return new ObjectRestResponse<>().error();
        }
    }


    @GetMapping("/edit")
    @ApiOperation("编辑资源")
    @ApiImplicitParam(value = "id",name="id",dataType = "integer",required = true,paramType = "query")
    public BaseResponse edit(Resource resource){
        try {
            if(!SystemModelEnum.valueOf(BaseContextHandler.getSysModl()).equals(SystemModelEnum.ADMIN)){
                return  new ObjectRestResponse<>().error("访问受限");
            }
            if(resource.getId()==null){
                resource.setCreateBy(BaseContextHandler.getUserID());
                resource.setCreateTime(new Date());
            }
            resource.setModBy(BaseContextHandler.getUserID());
            resource.setModTime(new Date());
            resourceService.save(resource);
            return new ObjectRestResponse<>().succeed();
        }catch (Exception e){
            return new ObjectRestResponse<>().error();
        }
    }
}
